/**
 * Agent Query Tests
 * 
 * Tests for streaming and single mode queries
 */

import { describe, test, expect, beforeAll } from 'bun:test';
import { executeStreamingQuery, executeSingleQuery } from '../src/agent/query';
import type { QueryRequest } from '../src/types';

describe('Agent Query', () => {
  beforeAll(() => {
    // Ensure ANTHROPIC_API_KEY is set
    if (!process.env.ANTHROPIC_API_KEY) {
      console.warn('⚠️  ANTHROPIC_API_KEY not set, skipping API tests');
    }
  });

  describe('Streaming Mode', () => {
    test('should execute streaming query', async () => {
      if (!process.env.ANTHROPIC_API_KEY) {
        console.log('⏭️  Skipping streaming test (no API key)');
        return;
      }

      const request: QueryRequest = {
        prompt: 'Say "Hello from streaming mode" and nothing else.',
        options: {
          model: 'claude-sonnet-4',
          includePartialMessages: true,
          maxTurns: 1,
        },
      };

      const responses = [];
      for await (const response of executeStreamingQuery(request)) {
        responses.push(response);
      }

      // Should have at least one response
      expect(responses.length).toBeGreaterThan(0);

      // Should have stream events or assistant messages
      const hasStreamEvents = responses.some(r => r.type === 'stream_event');
      const hasAssistantMessage = responses.some(r => r.type === 'assistant');

      expect(hasStreamEvents || hasAssistantMessage).toBe(true);
    }, 30000); // 30 second timeout

    test('should handle streaming errors gracefully', async () => {
      const request: QueryRequest = {
        prompt: '',
        options: {
          includePartialMessages: true,
        },
      };

      const responses = [];
      for await (const response of executeStreamingQuery(request)) {
        responses.push(response);
      }

      // Should have at least one response (likely an error)
      expect(responses.length).toBeGreaterThan(0);
    });
  });

  describe('Single Mode', () => {
    test('should execute single query', async () => {
      if (!process.env.ANTHROPIC_API_KEY) {
        console.log('⏭️  Skipping single query test (no API key)');
        return;
      }

      const request: QueryRequest = {
        prompt: 'Say "Hello from single mode" and nothing else.',
        options: {
          model: 'claude-sonnet-4',
          includePartialMessages: false,
          maxTurns: 1,
        },
      };

      const response = await executeSingleQuery(request);

      // Should have a response
      expect(response).toBeDefined();
      expect(response.type).toBeDefined();

      // Should be either assistant or result type
      expect(['assistant', 'result', 'error']).toContain(response.type);
    }, 30000); // 30 second timeout

    test('should handle single query errors gracefully', async () => {
      const request: QueryRequest = {
        prompt: '',
        options: {
          includePartialMessages: false,
        },
      };

      const response = await executeSingleQuery(request);

      // Should have a response
      expect(response).toBeDefined();
    });
  });

  describe('Mode Detection', () => {
    test('should default to streaming mode', () => {
      const request: QueryRequest = {
        prompt: 'test',
        options: {},
      };

      // includePartialMessages defaults to true (streaming mode)
      expect(request.options?.includePartialMessages !== false).toBe(true);
    });

    test('should respect explicit single mode', () => {
      const request: QueryRequest = {
        prompt: 'test',
        options: {
          includePartialMessages: false,
        },
      };

      expect(request.options.includePartialMessages).toBe(false);
    });
  });
});

